<?php
App::import('Vendor', 'php-excel-reader/excel_reader2'); 
class ImportsController extends AppController {

	var $name = 'Imports';
    
    function savetable($temp){
        //$data = new Spreadsheet_Excel_Reader('sinhvienk34.xls', true);
        //$temp = $data->dumptoarray();
        foreach($temp as $value){
                $this->Import->create();
                $query = "'',";
                for($x=1;$x<count($temp[1]);$x++)
                {   
                    $query= $query."'".$value[$x]."',";
                }  
                $x=count($temp[1]);
                $query=$query."'".$value[$x]."'";
                $this->Import->query("Insert into imports values($query)");
        }
    }
    function createcol($temp){
        $this->Import->query("TRUNCATE TABLE 'imports'");
        for($j=1;$j<=count($temp[1]);$j++)
        {
            $this->Import->query("ALTER TABLE imports DROP ".$temp[1][$j]);
        }
        for($j=1;$j<=count($temp[1]);$j++)
        {
            $this->Import->query("ALTER TABLE imports ADD ".$temp[1][$j]." varchar (255)");
        }
    }
    /**
	 * Display the content of example.xls
	 */
	function review() {
	    $data = new Spreadsheet_Excel_Reader('sinhvienk34.xls', true);
        $temp = $data->dumptoarray();        
        //ALTER TABLE `imports` ADD `q` INT NOT NULL
        //ALTER TABLE `imports` DROP `created`
 
        //$this->createcol($temp);
        for($j=1;$j<=count($temp[1]);$j++)
        {
            $this->Import->query("ALTER TABLE imports ADD ".$temp[1][$j]." varchar (255)");
        }
        $this->savetable($temp);
	    $this->set('data', $data);
	}
    
    /**
   * Read the excel content into an $arr and
   * print it to debug.log
   */
      function read_excel() {
    $data = new Spreadsheet_Excel_Reader('test.xls', true);
    $temp = $data->dumptoarray();
    $this->log($temp, 'debug');
      }
	function index() {
		$this->Import->recursive = 0;
		$this->set('imports', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid import', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('import', $this->Import->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Import->create();
			if ($this->Import->save($this->data)) {
				$this->Session->setFlash(__('The import has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The import could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid import', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Import->save($this->data)) {
				$this->Session->setFlash(__('The import has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The import could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Import->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for import', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Import->delete($id)) {
			$this->Session->setFlash(__('Import deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Import was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
